package cn.zh.stan.activity;

import android.content.Intent;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.R;
import cn.zh.stan.util.RegexUtils;
import cn.zh.stan.util.ToastUtil;
import okhttp3.Request;
import okhttp3.Response;

public class Renext extends BaseActivity {

    EditText renpass,recpass;

    Mycount mycount;
    Button reout_time,registn;
    String name;
    LinearLayout rnlin;
    ImageView rnback,rnimg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_renext);
        name=getIntent().getStringExtra("name");
        iniv();
         }


    public void iniv(){
        reout_time= (Button) findViewById(R.id.reout_time);
        mycount = new Mycount(60*1000,1000);
        mycount.start();
        renpass= (EditText) findViewById(R.id.renpass);
        recpass= (EditText) findViewById(R.id.recpass);

        registn= (Button) findViewById(R.id.registn);

        rnlin= (LinearLayout) findViewById(R.id.rnlin);
        rnback= (ImageView) findViewById(R.id.rnback);
        rnimg= (ImageView) findViewById(R.id.rnimg);

        reout_time.setOnClickListener(this);
        registn.setOnClickListener(this);
        rnback.setOnClickListener(this);
        rnimg.setOnClickListener(this);

        reout_time.setEnabled(false);

        renpass.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void afterTextChanged(Editable editable) {

                int len=editable.length();
                if (len>0){
                    rnimg.setVisibility(View.VISIBLE);
                    rnlin.setBackgroundResource(R.drawable.cprimbg);
                }else {
                    rnimg.setVisibility(View.INVISIBLE);
                    rnlin.setBackgroundResource(R.drawable.graybg);
                }
             }
           });
          }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.registn:
                getPos();
                break;
            case R.id.reout_time:
                mycount.start();
                reout_time.setEnabled(false);
                getPost();
                break;
            case R.id.rnimg:
                renpass.getText().clear();
                rnlin.setBackgroundResource(R.drawable.graybg);
                rnimg.setVisibility(View.INVISIBLE);
                break;
            case R.id.rnback:
                finish();
                break;
            }
        }


    public class Mycount extends CountDownTimer {
        public Mycount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
           }
        @Override
        public void onTick(long millisUntilFinished) {
            long myhour = (millisUntilFinished / 1000) / 3600;
            long myminute = ((millisUntilFinished / 1000) - myhour * 3600) / 60;
            long mysecond = millisUntilFinished / 1000 - myhour * 3600
                    - myminute * 60;
            reout_time.setText(mysecond+"后重发");
            }

        @Override
        public void onFinish() {
            reout_time.setText("重新发送");
            reout_time.setEnabled(true);
           }
         }


    public void getPos(){
        String pass=renpass.getText().toString();
        String cpass=recpass.getText().toString();
        OkHttpUtils.post(BaseUrl.regist)
                .params("username",name)
                .params("password",pass)
                .params("code",cpass)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                                boolean Flag=js.getBoolean("Flag");
                                if (Flag){
                                    String Msg=js.getString("Msg");
                                    ToastUtil.showLong(Renext.this,Msg);
                                    Intent intent=new Intent(Renext.this,Login.class );
                                    startActivity(intent);
                                    finish();
                                  }else {
                                    String Msg=js.getString("Msg");
                                    ToastUtil.showShort(Renext.this,Msg);
                                 }
                            }else {
                                String msg=js.getString("msg");
                                ToastUtil.showShort(Renext.this,msg);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }


            //发送验证码

    public void getPost() {

        boolean ismobile = RegexUtils.checkMobile(name);
        if (ismobile){
            OkHttpUtils.post(BaseUrl.pass_msg)
                    .params("moblie", name)
                    .execute(new StringCallback() {
                        @Override
                        public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                            System.out.println("------------->注册发送验证码==" + s);
                            try {
                                JSONObject js = new JSONObject(s);
                                boolean status = js.getBoolean("State");
                                if (status) {
                                    boolean Flag = js.getBoolean("Flag");
                                    boolean Mark = js.getBoolean("Mark");
                                    if (Flag) {
                                        String Msg = js.getString("Msg");
                                        ToastUtil.showShort(Renext.this, Msg);


                                    }else {
                                        String Msg = js.getString("Msg");
                                        ToastUtil.showShort(Renext.this, Msg);
                                    }

                                } else {
                                    String msg = js.getString("msg");
                                    ToastUtil.showLong(Renext.this, msg);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
        }else {
            ToastUtil.showLong(Renext.this,"手机号错误");
        }

    }

}
